<?php

/**
 * Accesses the tgp_feed_entries table in the thegridpaper database
 * @author panosbaroudjian
 *
 */
class Application_Model_DbTable_FeedEntries extends Zend_Db_Table_Abstract
{
    protected $_name = 'tgp_feed_entries';
    protected $_primary = 'feed_entry_id';
    protected $_dependentTables = array('Application_Model_DbTable_GridBoxes');
    protected $_referenceMap = array(
                                    'Feed' => array('columns' => array('feed_id'),
                                                    'refTableClass' => 'Application_Model_DbTable_Feeds',
                                                    'refColumns' => array('feed_id'),
                                                    'onDelete' => self::CASCADE));

    /**
     * Gets a feed entry by its id
     * @param int $id The id of the feed entry
     * @return NULL|array: An array containing the feed entry's row if found, NULL otherwise.
     */
    public function getById($id)
    {
        $id = (int)$id;
        $row = $this->find((int)$id);
        if (!$row)
        {
            return null;
        }
        return $row->toArray();
    }

    /**
     * Gets a feed by its unique RSS Id
     * @param int $feed_id The id of the feed
     * @param string $rss_entry_id The unique Id from the RSS
     * @return NULL|array: An array containing the feed entry's row if found, NULL otherwise.
     */
    public function getByRssEntryId($feed_id, $rss_entry_id)
    {
        $where = array();
        $where['feed_id = ?'] = $feed_id;
        $where['rss_entry_id = ?'] = $rss_entry_id;
        $row = $this->fetchRow($where);
        if (!$row)
            return null;
        return $row->toArray();
    }

    /**
     * Gets all the feed entries of an account
     * @param int $id The id of the account
     * @param int $minTimestamp Not used anymore
     * @return NULL|array: An array containing the rows if found, NULL otherwise.
     */
    public function getByAccountId($id, $minTimestamp)
    {
        $id = (int)$id;
        $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
            ->setIntegrityCheck(false)
            ->joinUsing('tgp_account_feeds', 'feed_id')
            ->join('tgp_accounts', 'tgp_accounts.account_id = tgp_account_feeds.account_id')
            ->where('tgp_accounts.account_id = ?', $id); // FIXME: uncomment to keep only the recent feeds
        //->where('publish_time > ?', $minTimestamp)
        $rows = $this->fetchAll($select);
        if (!$rows)
            return null;
        return $rows->toArray();
    }

    /**
     * Gets all the entries from a given feed
     * @param int $feedid The id of the feed
     * @return NULL|array: An array containing the rows if found, NULL otherwise.
     */
    public function getByFeedId($feedid)
    {
        $select = $this->select()->where('feed_id = ?', (int)$feedid);
        $row = $this->fetchAll($select);
        if (!$row)
        {
            return null;
        }
        return $row->toArray();
    }

    /**
     * Gets entries by their title
     * @param string $title The title of the entry
     * @return NULL|array: An array containing the feed entry's row if found, NULL otherwise.
     */
    public function getByTitle($title)
    {
        $select = $this->select()->where('title = ?', $title);
        $row = $this->fetchAll($select);
        if (!$row)
        {
            return null;
        }
        return $row->toArray();
    }

    /**
     * Inserts a new feed entry
     * @param unknown_type $rss_entry_id The unique RSS ID
     * @param unknown_type $feedid The id of the feed where the entry is taken from
     * @param unknown_type $title The title of the entry
     * @param unknown_type $link The link of the entry
     * @param unknown_type $description The description of the entry
     * @param unknown_type $author The author of the entry
     * @param unknown_type $publish_time The publish time of the entry
     * @param unknown_type $enclosure The enclosure (if any) of the entry
     * @return int: The id of the newly inserted feed
     */
    public function insertFeedEntry($rss_entry_id,
                                    $feedid,
                                    $title,
                                    $link,
                                    $description,
                                    $author,
                                    $publish_time,
                                    $enclosure)
    {
        $data = array('feed_id' => $feedid,
                    'rss_entry_id' => $rss_entry_id,
                    'title' => $title,
                    'link' => $link,
                    'description' => $description,
                    'author' => $author,
                    'publish_time' => $publish_time,
                    'enclosure' => $enclosure);
        return $this->insert($data);
    }

    /**
     * Updates an entry
     * @param unknown_type $id The id of the entry
     * @param unknown_type $rss_entry_id The unique RSS ID
     * @param unknown_type $feedid The id of the feed where the entry is taken from
     * @param unknown_type $title The title of the entry
     * @param unknown_type $link The link of the entry
     * @param unknown_type $description The description of the entry
     * @param unknown_type $author The author of the entry
     * @param unknown_type $publish_time The publish time of the entry
     * @param unknown_type $enclosure The enclosure (if any) of the entry
     */
    public function updateFeedEntry($id,
                                    $rss_entry_id,
                                    $feedid,
                                    $title,
                                    $link,
                                    $description,
                                    $author,
                                    $publish_time,
                                    $enclosure)
    {
        $where = $this->getAdapter()->quoteInto('feed_entry_id = ?', $id);
        $data = array('rss_entry_id' => $rss_entry_id,
                    'feed_id' => $feedid,
                    'title' => $title,
                    'link' => $link,
                    'description' => $description,
                    'author' => $author,
                    'publish_time' => $publish_time,
                    'enclosure' => $enclosure);
        $this->update($data, $where);
    }

    public function getMostRecentEntryFromFeed($feed_id)
    {
        $select = $this->select()->where('feed_id = ?', $feed_id);
        $row = $this->fetchAll($select);
        if (!$row || $row->count() == 0)
        {
            return null;
        }
        return $row->current()->toArray();
    }

    /**
     * Deletes an entry
     * @param int $id The id of the entry to be deleted
     */
    public function deleteFeedEntry($id)
    {
        $where = $this->getAdapter()->quoteInto('feed_entry_id = ?', $id);
        $this->delete($where);
    }
}

